//
//  IGCActivityVC.m
//  jingdongfang
//
//  Created by BOE on 2018/5/10.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGActivityVC.h"
#import "IGCommunityListApi.h"
#import "IGEmptyView.h"
#import "IGActivityCell.h"
#import "IGActivityListApi.h"
#import "IGAttentionActivityApi.h"
#import "IGExhibitionVC.h"
#import "IGCompetitionVC.h"

@interface IGActivityVC ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, strong) UITableView *actTbl;
@property (nonatomic, strong) NSMutableArray *actMuArr;

@property (nonatomic, strong) IGActivityListApi *actListApi;
@property (nonatomic, copy) NSString *queryTime;
@property (nonatomic, assign) NSInteger page;

@end

@implementation IGActivityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initData];
    [self createUI];
    [self requestData];
}

- (void)initData {
    self.actMuArr = [NSMutableArray array];
    self.actListApi = [IGActivityListApi new];
    self.queryTime = @"0";
    self.page = 1;
}

- (void)createUI {
    [self.view addSubview:self.actTbl];
    [self.actTbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(17.5);
        make.right.mas_equalTo(-17.5);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(KSCREENHEIGHT - SafeBottomMagin - 49 - NavigationMagin);
    }];
    
    __weak typeof(self) weakSelf = self;
    [self.actTbl ig_setHeaderWithRefreshingBlock:^{
        weakSelf.queryTime = @"0";
        weakSelf.page = 1;
        [weakSelf requestData];
    }];
}
- (void)requestData {
    self.actListApi.queryTime = self.queryTime;
    self.actListApi.page = self.page;
    self.actListApi.type = self.type;
    __weak typeof(self) weakSelf = self;
    [self.actListApi startWithDecodeClass:[IGActivityListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [weakSelf.actTbl ig_endHeaderRefreshing];
        [weakSelf.actTbl ig_endFooterRefreshing];
        
        if (responseObject) {
            IGActivityListResp *resp = responseObject;
            IGActivityListModel *model = resp.data;
            
            NSArray *tmpActivityArr = model.activityList;
            if (tmpActivityArr && tmpActivityArr.count > 0) {
                [weakSelf setupFooter];
            } else {
//                if (weakSelf.page == 1) {
//                    [weakSelf.actTbl reloadData];
//                    return;
//                }
                [weakSelf.actTbl ig_endFooterRefreshingWithNoMoreData];
            }
            
            if (weakSelf.page == 1) {
                weakSelf.actMuArr = [NSMutableArray arrayWithArray:tmpActivityArr];
            } else {
                [weakSelf.actMuArr addObjectsFromArray:tmpActivityArr];
            }
            weakSelf.queryTime = model.queryTime;
            
            [weakSelf.actTbl reloadData];
        } else {
            [weakSelf.actTbl reloadData];
        }
    }];
}

- (void)setupFooter {
    __weak typeof(self) weakSelf = self;
    [self.actTbl ig_setFooterWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf requestData];
    }];
}

#pragma mark - 懒加载
- (UITableView *)actTbl {
    if (!_actTbl) {
        _actTbl = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _actTbl.delegate = self;
        _actTbl.dataSource = self;
        _actTbl.showsVerticalScrollIndicator = NO;
        _actTbl.showsHorizontalScrollIndicator = YES;
        if ([[[UIDevice currentDevice] systemVersion] floatValue]>= 11.0)
        {
            _actTbl.estimatedRowHeight = 0;
            _actTbl.estimatedSectionHeaderHeight = 0;
            _actTbl.estimatedSectionFooterHeight = 0;
        }
        _actTbl.emptyDataSetDelegate = self;
        _actTbl.emptyDataSetSource = self;
        _actTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _actTbl;
}

- (void)attention:(IGActivityModel *)model {
    __weak typeof(self) weakSelf = self;
    IGAttentionActivityApi *attentApi = [[IGAttentionActivityApi alloc]init];
    attentApi.operatorType = [model.attentionState boolValue]?@"1":@"0";
    attentApi.activityId = model.aid;
    [attentApi startWithDecodeClass:[IGAttentionActivityResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            if ([model.attentionState boolValue]) {
                model.attentionState = @"0";
            } else {
                model.attentionState = @"1";
            }
        } else {
            if ([model.attentionState boolValue]) {
                [self presentMessageTips:NSLocalizedString(@"Follow Failed", nil)];
            } else {
                [self presentMessageTips:NSLocalizedString(@"Cancel Failed", nil)];
            }
        }
        [weakSelf.actTbl reloadData];
    }];
}

#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.actMuArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IGActivityModel *model = self.actMuArr[indexPath.row];
    IGActivityCell *cell = [IGActivityCell cellWithTableView:tableView];
    cell.actModel = model;
    __weak typeof(self) weakSelf = self;
    cell.attentionBlock = ^{
        [weakSelf attention:model];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    IGActivityModel *model = self.actMuArr[indexPath.row];
    CGFloat h = [IGActivityCell cellHeight:model];
    return h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    IGActivityModel *model = self.actMuArr[indexPath.row];
    if ([model.type isEqualToString:@"1"]) {        ///>展览
        IGExhibitionVC *exhibitionVC = [[IGExhibitionVC alloc] init];
        exhibitionVC.activityId = model.aid;
        [UIViewController ig_pushViewController:exhibitionVC animated:YES];
    } else if ([model.type isEqualToString:@"2"]) { ///>比赛
        IGCompetitionVC *competitionVC = [[IGCompetitionVC alloc] init];
        competitionVC.activityId = model.aid;
        [UIViewController ig_pushViewController:competitionVC animated:YES];
    }
}

#pragma mark -Empty
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    IGEmptyView *emptyView = [[IGEmptyView alloc] init];
    emptyView.title = NSLocalizedString(@"No Data", nil);
    emptyView.requestApi = self.actListApi;
    return emptyView;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView {
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

